﻿using System;
using System.Collections.Generic;
using System.Text;

namespace Pixysoft
{
    /// <summary>
    /// 如果输入地址 不能最后以 \ 结尾
    /// 输入项可以用单引号括起来 防止空格出现判断错误
    /// </summary>
    public class ArgumentCoder
    {
        List<KeyValuePair<string, string>> list = new List<KeyValuePair<string, string>>();

        public ArgumentCoder(string[] args)
        {
            if (args == null)
                return;

            if (args.Length == 0)
                return;

            foreach (string arg in args)
            {
                if (string.IsNullOrEmpty(arg))
                    continue;

                string[] keyvalues = arg.Split(new char[] { '=' }, StringSplitOptions.RemoveEmptyEntries);

                if (keyvalues.Length != 2)
                    continue;

                string key = keyvalues[0].Trim().ToUpper();

                //10-02-12 bug! 区分大小写！

                string value = keyvalues[1].Trim();

                //string value = keyvalues[1].Trim().ToUpper();

                list.Add(new KeyValuePair<string, string>(key, value));
            }
        }

        public string GetValue(string key)
        {
            foreach (KeyValuePair<string, string> pair in list)
            {
                if (pair.Key.Equals(key, StringComparison.OrdinalIgnoreCase))
                    return pair.Value;
            }

            return null;
        }

        public bool Contains(string key)
        {
            foreach (KeyValuePair<string, string> pair in list)
            {
                if (pair.Key.Equals(key, StringComparison.OrdinalIgnoreCase))
                    return true;
            }

            return false;
        }

        public string[] GetKeys()
        {
            List<string> keys = new List<string>();

            foreach (KeyValuePair<string, string> pair in list)
            {
                if (keys.Contains(pair.Key))
                    continue;

                keys.Add(pair.Key);
            }

            return keys.ToArray();
        }
    }
}
